﻿using System;
using System.Collections.Generic;
using IFRS.DataAccess.DAL;
using IFRS.Models;
using IFRS.Web;
using IFRS_Global;

public partial class IFRS_WIN23_4A : PageBase
{
    #region === Event ===

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewMenu();
            BindControl();
            BindData();
        }
        else
            ClientScript.RegisterHiddenField("isPostBack", "1");
    }
    protected void btnSAVE_Click(object sender, EventArgs e)
    {
        SaveData();
    }
    protected void btnCANCEL_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/IFRS/WIN23_1S.aspx");
    }
    protected void btnSAVE_NEW_Click(object sender, EventArgs e)
    {
        SaveData(true);
    }
    protected void btnCLEAR_Click(object sender, EventArgs e)
    {
        Response.Redirect(ResolveUrl(Request.Url.AbsolutePath));
    }

    #endregion

    #region === Method ===

    private void BindControl()
    {
        btnSAVE.ValidationGroup = "Master";
        btnSAVE_NEW.ValidationGroup = btnSAVE.ValidationGroup;

        reqDataType.SetRequiredField(lblDataType, rbtDataType, btnSAVE); reqDataType.CssClass = "required_inline";
        reqSourceDataID.SetRequiredField(lblSourceDataID, ddlSourceDataID, btnSAVE);
        reqFeeAndCostTypeName.SetRequiredField(lblFeeAndCostTypeName, txtFeeAndCostTypeName, btnSAVE);
        reqFeeAndCostTypeNM.SetRequiredField(lblFeeAndCostTypeNM, txtFeeAndCostTypeNM, btnSAVE);

        btnFeeAndCostTypeName.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/FeeAndCostType.aspx") + "','700','550');");
        txtFeeAndCostTypeName.AddControl(hddFeeAndCostTypeID.ClientID);
        txtFeeAndCostTypeName.AddControl(txtFeeAndCostTypeName.ClientID);
        txtFeeAndCostTypeName.ControlSearch = txtFeeAndCostTypeName.ClientID;
        txtFeeAndCostTypeName.SearchKey = "FeeAndCostType_oFeeAndCostTypeID_oFeeAndCostTypeCode";
        
        btnFeeAndCostTypeNM.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/OrgFeeAndCostType.aspx") + "','700','550');");
        txtFeeAndCostTypeNM.AddControl(hddOrgFeeAndCostTypeID.ClientID);
        txtFeeAndCostTypeNM.AddControl(txtFeeAndCostTypeNM.ClientID);
        txtFeeAndCostTypeNM.ControlSearch = txtFeeAndCostTypeNM.ClientID;
        txtFeeAndCostTypeNM.SearchKey = "OrgFeeAndCostType_oCBT_CUST_BUSINESS_TYPE_ID_oCBT_DESC_LEV1";

        ddlSourceDataID.SetData(DDL_TYPE.SourceData, string.Empty, false, true);
    }

    private void BindData()
    {
        if (Convert.ToString(Request.QueryString["id"]) != string.Empty)
        {
            
            var id = Convert.ToInt32(Request.QueryString["id"]);
            var dt = new SPR_FEEANDCOSTMAPPING().SEARCH_BYID(id);
            if (dt != null && dt.Rows.Count > 0)
            {
                var list = new List<FeeAndCostMapping>();
                ConvertHelper.ConvertDataTableToObject(dt, list);

                rbtDataType.SelectedValue = list[0].DataType.AsString();
                ddlSourceDataID.SelectedValue = list[0].SourceDataID.AsString();

                hddFeeAndCostTypeID.Value = list[0].FeeAndCostTypeID.AsString();
                txtFeeAndCostTypeName.Text = list[0].FeeAndCostTypeName;

                hddOrgFeeAndCostTypeID.Value = list[0].OrgFeeAndCostTypeID.AsString();
                txtFeeAndCostTypeNM.Text = list[0].FeeAndCostTypeNM;
            }
        }
    }

    private void ViewMenu()
    {
        MenuTitleBar = Resources.ifrsMenu.WIN23_4A;
    }
    private void SaveData(bool saveNew = false)
    {
        if (!Page.IsValid) return;

        var dal = new SPR_FEEANDCOSTMAPPING();
        var models = new FeeAndCostMapping();

        models.DataType = rbtDataType.SelectedValue.AsInt32();
        models.SourceDataID = ddlSourceDataID.SelectedValue.AsInt32();
        models.FeeAndCostTypeID = hddFeeAndCostTypeID.Value.AsInt32();
        models.OrgFeeAndCostTypeID = hddOrgFeeAndCostTypeID.Value.AsInt32();

        models.SetStandardField();

        var id = dal.INSERT(models);
        if (id > 0)
        {
            if (saveNew)
                JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(Request.Url.AbsolutePath));
            else
                JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(string.Format("~/IFRS/WIN23_2E.aspx?&id={0}", id)));
        }
        else // Duplicate
            Utility.AlertMessage(Page,id == -1 ? Resources.ifrsResource.Duplicate : Resources.ifrsResource.CanNotSaveData);
    }

    #endregion
}